{*******************************************************************************
* The contents of this file are used with permission, subject to the Mozilla   *
* Public License Version 1.1 (the "License"); you may not use this file except *
* in compliance with the License. You may obtain a copy of the License at      *
* http://www.mozilla.org/MPL/                                                  *
*                                                                              *
* Software distributed under the License is distributed on an "AS IS" basis,   *
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for *
* the specific language governing rights and limitations under the License.    *
*                                                                              *
* The Original Code is distributed as part of the "mediate" product and is     *
* Copyright (C) @home media limited. All Rights Reserved.                      *
*                                                                              *
* Email: support@athomemedia.co.uk                                             *
* Web:   http://www.athomemedia.co.uk                                          *
*******************************************************************************}

unit LanguageDescriptor;

interface

uses Classes, Languages, Descriptor;

const
  FILE_LANGUAGE_DESCRIPTOR = 'LanguageDescriptor.xml';

type
  TAHMLanguageDescriptor = class(TAHMDescriptor)
  private
    FLanguageManager: TAHMLanguageManager;
    procedure ParseSupportedLanguageList;
    procedure ParseNativeLanguage;
  protected
    procedure SetBaseDescriptor(Base: TAHMDescriptor); override;
    function GetSaveRequired: Boolean; override;
    procedure ParseXML; override;
    procedure SaveAsXML(Lines: TStrings); override;
  public
    constructor Create; override;
    destructor Destroy; override;
    property LanguageManager: TAHMLanguageManager read FLanguageManager;
    procedure Initialise; override;
    procedure Assign(Source: TAHMDescriptor); override;
    procedure Sort; override;
  end;


implementation

uses TagNames, XMLTags, ErrorCodes, SysUtils, XMLHelper;

constructor TAHMLanguageDescriptor.Create;
begin
  inherited Create;

  FFileName := FILE_LANGUAGE_DESCRIPTOR;
  FRootTagName := TAG_LANGUAGE_DESCRIPTOR;

  FLanguageManager := TAHMLanguageManager.Create;
end;

destructor TAHMLanguageDescriptor.Destroy;
begin
  FLanguageManager.Free;

  inherited Destroy;
end;

procedure TAHMLanguageDescriptor.SetBaseDescriptor(Base: TAHMDescriptor);
begin
  // Set base manager property for each of our list objects
  with Base as TAHMLanguageDescriptor do
    Self.FLanguageManager.BaseManager := LanguageManager;
end;

function TAHMLanguageDescriptor.GetSaveRequired: Boolean;
begin
  // Save required if any of the lists are populated
  Result := FLanguageManager.Languages.RequiresSaving;
end;

procedure TAHMLanguageDescriptor.ParseXML;
var
  RootNode: TAHMXMLNode;
begin
  // What nodes do we have here?
  RootNode := FHelper.CurrentNode;
  FParseNode := nil;

  while FHelper.NextElement(RootNode, FParseNode) do
  begin
    if FHelper.CurrentElement = TAG_SUPPORTED_LANGUAGES then
      ParseSupportedLanguageList
    else if FHelper.CurrentElement = TAG_NATIVE_LANGUAGE then
      ParseNativeLanguage
    else if FHelper.CurrentElement = TAG_LANGUAGES then
      FLanguageManager.Languages.ParseFromXML(FHelper)
    else
      LogParsingError(THM_PRS_LNG_FOUND_UNKNOWN_CONTENT_TAG);
  end;
end;

procedure TAHMLanguageDescriptor.SaveAsXML(Lines: TStrings);
begin
  // Write all language descriptor lists to XML file
  FLanguageManager.SaveAsXML(Lines);
end;

procedure TAHMLanguageDescriptor.ParseSupportedLanguageList;
begin
  // No longer required
end;

procedure TAHMLanguageDescriptor.ParseNativeLanguage;
var
  sName: String;
  LangNode: TAHMXMLNode;
begin
  // Check the language tag is there
  LangNode := nil;
  if not FHelper.FindElement(FParseNode, LangNode, TAG_LANGUAGE) then
    LogParsingError(THM_PRS_LNG_MISSING_NATIVE_LANG_TAG);

  // Get the name and inform language manager
  sName := FHelper.GetAttribute(ATTR_NAME);
  FLanguageManager.NativeLanguageName := sName;
end;

procedure TAHMLanguageDescriptor.Initialise;
begin
  inherited;

  // Initialise preferred language
  FLanguageManager.Initialise;
end;

procedure TAHMLanguageDescriptor.Assign(Source: TAHMDescriptor);
begin
  inherited;

  // Copy properties from language descriptor
  with Source as TAHMLanguageDescriptor do
  begin
    Self.LanguageManager.Assign(LanguageManager);
  end;
end;

procedure TAHMLanguageDescriptor.Sort;
begin
  inherited;

{$IFDEF DESIGNER}
  FLanguageManager.Languages.SortObjects;
{$ENDIF}
end;

end.

